<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Libs\Storage;


use Illuminate\Support\Facades\Storage;

class StorageFile
{
    private $disk;

    function __construct( $name = NULL )
    {
        $this->disk = Storage::disk((string) $name);
    }

    /**
     * 赋值
     *
     * @param string|NULL $name
     *
     * @return \Illuminate\Contracts\Filesystem\Filesystem
     */
    function disk( string $name = NULL )
    {
        return $this->disk = Storage::disk((string) $name);
    }

    /**
     * 查询文件
     *
     * @param string $path
     *
     * @return bool
     */
    function exists( string $path )
    {
        return $this->disk->exists((string) $path);
    }


    /**
     * 删除文件
     *
     * @param string $paths
     *
     * @return bool
     */
    function delete( string $paths )
    {
        return $this->disk->delete((string) $paths);
    }

    /**
     * 创建文件或修改文件
     *
     * @param string $path
     * @param        $contents
     * @param array  $options
     *
     * @return bool
     */
    function put( string $path, $contents, $options = [] )
    {
        return $this->disk->put((string) $path, $contents, $options);
    }

    /**
     * 获取文件
     *
     * @param string $path
     *
     * @return string
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    function get( string $path )
    {
        return $this->disk->get((string) $path);
    }

    /**
     * 获取文件大小
     *
     * @param string $path
     *
     * @return int
     */
    function size( string $path )
    {
        return $this->disk->size((string) $path);
    }

    /**
     *
     * 获取创建时间
     *
     * @param string $path
     *
     * @return int
     */
    function time( string $path )
    {
        return $this->disk->lastModified((string) $path);
    }

    /**
     * 获取目录下所有子目录
     *
     * @param string|NULL $directories
     * @param bool        $recursive
     *
     * @return array
     */
    function directories( string $directories = NULL, $recursive = FALSE )
    {
        return $this->disk->directories((string) $directories, (bool) $recursive);
    }

    /**
     * 获取文件地址
     *
     * @param string $path
     *
     * @return mixed
     */
    function url( string $path )
    {
        return $this->disk->url((string) $path);
    }

    /**
     * 获取本地文件地址
     *
     * @param string $path
     *
     * @return string
     */
    function fileUrl( string $path )
    {
        return $this->disk->getDriver()->getAdapter()->getPathPrefix() . (string) $path;
    }

    /**
     * 创建目录
     *
     * @param string $path
     *
     * @return bool
     */
    function makeDirectory( string $path )
    {
        return $this->disk->makeDirectory((string) $path);
    }

    /**
     * 删除目录
     *
     * @param string $path
     *
     * @return bool
     */
    function deleteDirectory( string $path )
    {
        return $this->disk->deleteDirectory((string) $path);
    }

    /**
     * 获取所有文件
     *
     * @param string|NULL $directory
     *
     * @return array
     */
    function allFiles( string $directory = NULL )
    {
        return $this->disk->allFiles((string) $directory);
    }

    /**
     * 下载
     *
     * @param string $path
     *
     * @return mixed
     */
    function download( string $path )
    {
        return $this->disk->download((string) $path);
    }

    /**
     * 新增
     *
     * @param string $path
     * @param string $data
     *
     * @return int
     */
    function append( string $path, string $data = '' )
    {
        return $this->disk->append((string) $path, (string) $data);
    }


    /**
     * 复制
     *
     * @param string $form
     * @param string $to
     *
     * @return bool
     */
    function copy( string $form, string $to )
    {
        return $this->disk->copy((string) $form, (string) $to);
    }

    /**
     * 移动
     * @param string $form
     * @param string $to
     *
     * @return bool
     */
    function move( string $form, string $to )
    {
        return $this->disk->move((string) $form, (string) $to);
    }
}
